﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace TiDiSun
{
    public partial class Update_Sanpham_Pin : Form
    {
        public Update_Sanpham_Pin()
        {
            InitializeComponent();
        }

        public int id;

        private void Update_Sanpham_Pin_Load(object sender, EventArgs e)
        {
            try
            {
                id = int.Parse(this.Tag.ToString());
                PinBUS pinBus = new PinBUS();
                SanPham_DaTa_Pin pin = new SanPham_DaTa_Pin();
                pin = pinBus.load_pin_id(id);
                txtKichThuocPin.Text = pin.KichThuoc;
                txtVonMaxPin.Text = pin.VMax.ToString();
                txtVonMinPin.Text = pin.VMin.ToString();
                txtAmpeMaxPin.Text = pin.AMax.ToString();
                txtAmpeMinPin.Text = pin.AMin.ToString();

                LoaiPinBUS loaipinBUs = new LoaiPinBUS();
                List<LoaiPinDTO> loaipin = new List<LoaiPinDTO>();
                loaipin = loaipinBUs.load_loaiPin();
                cbbloaipin_sanpham.DataSource = loaipin;
                cbbloaipin_sanpham.ValueMember = "MaLoaiPin";
                cbbloaipin_sanpham.DisplayMember = "TenLoai";
                cbbloaipin_sanpham.SelectedValue = pin.LoaiPin;
            }
            catch { this.Close(); }
        }

        private void btnThemPin_Click(object sender, EventArgs e)
        {
            if (txtKichThuocPin.Text == "Hãy nhập" || txtVonMaxPin.Text == "Hãy nhập" || txtVonMinPin.Text == "Hãy nhập" || txtAmpeMaxPin.Text == "Hãy nhập" || txtAmpeMinPin.Text == "Hãy nhập")
            {
                SanPham_StatusThemPin();
                return;
            }

            SanPham_DaTa_Pin pin = new SanPham_DaTa_Pin();
            pin.id = id;
            pin.KichThuoc = txtKichThuocPin.Text;
            pin.VMax = float.Parse(txtVonMaxPin.Text);
            pin.VMin = float.Parse(txtVonMinPin.Text);
            pin.AMax = float.Parse(txtAmpeMaxPin.Text);
            pin.AMin = float.Parse(txtAmpeMinPin.Text);
            pin.LoaiPin = int.Parse(cbbloaipin_sanpham.SelectedValue.ToString());

            try
            {
                PinBUS pinBus = new PinBUS();
                pinBus.Update_SanPham_Pin(pin);
                this.Close();
            }
            catch { this.Close(); }
        }

        private void SanPham_StatusThemPin()
        {
            if (txtKichThuocPin.Text == "Hãy nhập")
            {
                txtKichThuocPin.Text = "Hãy nhập";
                txtKichThuocPin.ForeColor = System.Drawing.Color.Red;                
            }
            if (txtVonMaxPin.Text == "Hãy nhập")
            {
                txtVonMaxPin.Text = "Hãy nhập";
                txtVonMaxPin.ForeColor = System.Drawing.Color.Red;
            }
            if (txtVonMinPin.Text == "Hãy nhập")
            {
                txtVonMinPin.Text = "Hãy nhập";
                txtVonMinPin.ForeColor = System.Drawing.Color.Red;
            }
            if (txtAmpeMaxPin.Text == "Hãy nhập")
            {
                txtAmpeMaxPin.Text = "Hãy nhập";
                txtAmpeMaxPin.ForeColor = System.Drawing.Color.Red;
            }
            if (txtAmpeMinPin.Text == "Hãy nhập")
            {
                txtAmpeMinPin.Text = "Hãy nhập";
                txtAmpeMinPin.ForeColor = System.Drawing.Color.Red;
            }
        }

        private void txtKichThuocPin_TextChanged(object sender, EventArgs e)
        {
            txtKichThuocPin.ForeColor = System.Drawing.Color.Black;
        }

        private void txtVonMaxPin_TextChanged(object sender, EventArgs e)
        {
            txtVonMaxPin.ForeColor = System.Drawing.Color.Black;
        }

        private void txtVonMinPin_TextChanged(object sender, EventArgs e)
        {
            txtVonMinPin.ForeColor = System.Drawing.Color.Black;
        }

        private void txtAmpeMaxPin_TextChanged(object sender, EventArgs e)
        {
            txtAmpeMaxPin.ForeColor = System.Drawing.Color.Black;
        }

        private void txtAmpeMinPin_TextChanged(object sender, EventArgs e)
        {
            txtAmpeMinPin.ForeColor = System.Drawing.Color.Black;
        }

        private void txtKichThuocPin_Leave(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtKichThuocPin.Text))
            {
                txtKichThuocPin.Text = "Hãy nhập";
                txtKichThuocPin.ForeColor = System.Drawing.Color.DarkGray;
            }
        }

        private void txtVonMaxPin_Leave(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtVonMaxPin.Text))
            {
                txtVonMaxPin.Text = "Hãy nhập";
                txtVonMaxPin.ForeColor = System.Drawing.Color.DarkGray;
            }
        }

        private void txtVonMinPin_Leave(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtVonMinPin.Text))
            {
                txtVonMinPin.Text = "Hãy nhập";
                txtVonMinPin.ForeColor = System.Drawing.Color.DarkGray;
            }
        }

        private void txtAmpeMaxPin_Leave(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtAmpeMaxPin.Text))
            {
                txtAmpeMaxPin.Text = "Hãy nhập";
                txtAmpeMaxPin.ForeColor = System.Drawing.Color.DarkGray;
            }
        }

        private void txtAmpeMinPin_Leave(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtAmpeMinPin.Text))
            {
                txtAmpeMinPin.Text = "Hãy nhập";
                txtAmpeMinPin.ForeColor = System.Drawing.Color.DarkGray;
            }
        }

        private void txtKichThuocPin_Click(object sender, EventArgs e)
        {
            if (txtKichThuocPin.Text == "Hãy nhập")
            {
                txtKichThuocPin.Text = "";
                txtKichThuocPin.ForeColor = System.Drawing.Color.Black;
            }
        }

        private void txtVonMaxPin_Click(object sender, EventArgs e)
        {
            if (txtVonMaxPin.Text == "Hãy nhập")
            {
                txtVonMaxPin.Text = "";
                txtVonMaxPin.ForeColor = System.Drawing.Color.Black;
            }
        }

        private void txtVonMinPin_Click(object sender, EventArgs e)
        {
            if (txtVonMinPin.Text == "Hãy nhập")
            {
                txtVonMinPin.Text = "";
                txtVonMinPin.ForeColor = System.Drawing.Color.Black;
            }
        }

        private void txtAmpeMaxPin_Click(object sender, EventArgs e)
        {
            if (txtAmpeMaxPin.Text == "Hãy nhập")
            {
                txtAmpeMaxPin.Text = "";
                txtAmpeMaxPin.ForeColor = System.Drawing.Color.Black;
            }
        }

        private void txtAmpeMinPin_Click(object sender, EventArgs e)
        {
            if (txtAmpeMinPin.Text == "Hãy nhập")
            {
                txtAmpeMinPin.Text = "";
                txtAmpeMinPin.ForeColor = System.Drawing.Color.Black;
            }
        }

        private void txtKichThuocPin_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) && e.KeyChar != '.')
                e.Handled = true;
        }

        private void txtVonMaxPin_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) && e.KeyChar != '.')
                e.Handled = true;
        }

        private void txtVonMinPin_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) && e.KeyChar != '.')
                e.Handled = true;
        }

        private void txtAmpeMaxPin_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) && e.KeyChar != '.')
                e.Handled = true;
        }

        private void txtAmpeMinPin_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) && e.KeyChar != '.')
                e.Handled = true;
        }

        
        















    }
}
